package com.sdk.pay.transfer.services;

import android.app.IntentService;
import android.content.Intent;

import com.sdk.pay.transfer.MainActivity;
import com.sdk.pay.transfer.model.WECHATBean;
import com.sdk.pay.transfer.provider.ProviderHelper;
import com.sdk.pay.transfer.utils.ActivityHelper;
import com.sdk.pay.transfer.utils.Generation;

import static com.sdk.pay.transfer.utils.Generation.formatStr;

public class ProductDataService extends IntentService {
    private static final String TAG = "ProductDataService";
    private String mPlatForm;
    private String mWechatNum = "";
    private String mShopCode = "";

    public ProductDataService() {
        super(TAG);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MainActivity.instance.log("service onCreate");
    }

    @Override
    protected void onHandleIntent(final Intent intent) {
        try {
            mPlatForm = intent.getStringExtra("platform");
            String money = intent.getStringExtra("money");
            int count = intent.getIntExtra("count", 0);
            mWechatNum = intent.getStringExtra("platNum");
            mShopCode = intent.getStringExtra("shopCode");
            long currentIndex = Generation.strToNum(intent.getStringExtra("currentIndex"));
            insertNewProductData(money, count, currentIndex);
        } catch (Exception e) {
            e.printStackTrace();
            MainActivity.instance.log("service intent param error");
        }
    }

    private void insertNewProductData(String money, int count, long currentIndex) {
        for (int i = 1; i <= count; i++) {
            String reason = mPlatForm + mShopCode + mWechatNum + formatStr(money)
                    + formatStr(Generation.numToStr(currentIndex + i));
            WECHATBean bean = new WECHATBean();
            bean.setMoney(money);
            bean.setReason(reason);
            ProviderHelper.insertWX("", bean.money, bean.reason, "0", this);
            MainActivity.instance.log("insert bean:" + bean.money + "," + bean.reason);
        }
        ActivityHelper.startQRActivity();
    }
}